package com.four.controller;

import com.four.pojo.*;
import com.four.service.IStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller("staffController")

public class StaffController {
    @Autowired
    @Qualifier(value = "staffService")
    private IStaffService iStaffService;
    @RequestMapping("/findAllStaff")
    public String findAllStaff(Model model){
        List<Staff> list=iStaffService.findAllStaff();;
        model.addAttribute("list",list);
        return "staff_List.jsp";
    }
    @RequestMapping("findStaff")
    public String findStaff(Model model , String s ){
        if (s.equals("全部员工")) {
            return "redirect:findAllStaff";
        }else if (s.equals("在职员工")) {
            return "redirect:findZz";
        }else if (s.equals("离职员工")) {
            return "redirect:findLz";
        }
        return "";
    }

    @RequestMapping("findZz")
    public String findZz(Model model){
        List<Staff> list = iStaffService.findZz();
        model.addAttribute("list" , list);
        return "staff_List.jsp";
    }

    @RequestMapping("findLz")
    public String findLz(Model model){
        List<Staff> list = iStaffService.findLz();
        model.addAttribute("list" , list);
        return "staff_List.jsp";
    }
    @RequestMapping("/findByIdStaff")
    public String findByIdStaff(Model model,int staff_id){
        Staff staff=iStaffService.findByIdStaff(staff_id);
        model.addAttribute("staff",staff);
        return "update_Staff.jsp";
    }
    @RequestMapping("/updateStaff")
    public String updateStaff(Staff staff){
        iStaffService.updateStaff(staff);
        return "redirect:findAllStaff";
    }
    @RequestMapping("/addStaff")
    public String addStaff(Staff staff){
        iStaffService.addStaff(staff);
        return "redirect:findAllStaff";
    }
    @RequestMapping("/findByIdS")
    public String findByIdS(Model model,int staff_id){
        Staff staff=iStaffService.findByIdS(staff_id);
        model.addAttribute("staff",staff);
        List<Product> allProduct1 = iStaffService.findAllProduct1(staff_id);
        int tc1=0;
        int tc=0;
        if (allProduct1.size()>0){
            for(Product a:allProduct1){
                tc1+=a.getSales();
                tc+=a.getSales();
            }
        }
        model.addAttribute("allProduct1",allProduct1);
        model.addAttribute("tc1",tc1);
        List<Service> allService2 = iStaffService.findAllService2(staff_id);
        int tc2=0;
        if (allService2.size()>0){
            for(Service a:allService2){
                tc2+=a.getService_tc();
                tc+=a.getService_tc();
            }
        }
        model.addAttribute("tc2",tc2);
        model.addAttribute("allService2",allService2);
        List<Card> allCard3 = iStaffService.findAllCard3(staff_id);
        int tc3=0;
        if (allCard3.size()>0){
            for(Card a:allCard3){
                tc3+=a.getCard_tc();
                tc+=a.getCard_tc();
            }
        }
        model.addAttribute("tc3",tc3);
        model.addAttribute("allCard3",allCard3);
        List<Card> allNewscard4 = iStaffService.findAllNewscard4(staff_id);
        int tc4=0;
        if (allNewscard4.size()>0){
            for(Card a:allNewscard4){
                tc4+=a.getCard_tc();
                tc+=a.getCard_tc();
            }
        }
        model.addAttribute("tc4",tc4);
        model.addAttribute("allNewscard4",allNewscard4);
        model.addAttribute("tc",tc);
//        int i = iStaffService.updateZService(service );
//        model.addAttribute("i",i);
//        int i1 = iStaffService.updateZProduct(product);
//        model.addAttribute("i1",i1);
//        int i2 = iStaffService.updateZCard(card);
//        model.addAttribute("i2",i2);
//        int i3 = iStaffService.updateZNewscard(card);
//        model.addAttribute("i3",i3);
        return "staff_Achievement.jsp";
    }
    @PostMapping("/findstaff_TC")
    public String findstaff_TC(Model model,FIndstaff_TC fIndstaff_tc){
        Staff staff=iStaffService.findByIdS2(fIndstaff_tc);
        model.addAttribute("staff",staff);
        List<Product> allProduct1 = iStaffService.findAllProduct12(fIndstaff_tc);
        int tc1=0;
        int tc=0;
        if (allProduct1.size()>0){
            for(Product a:allProduct1){
                tc1+=a.getSales();
                tc+=a.getSales();
            }
        }
        model.addAttribute("allProduct1",allProduct1);
        model.addAttribute("tc1",tc1);
        List<Service> allService2 = iStaffService.findAllService22(fIndstaff_tc);
        int tc2=0;
        if (allService2.size()>0){
            for(Service a:allService2){
                tc2+=a.getService_tc();
                tc+=a.getService_tc();
            }
        }
        model.addAttribute("tc2",tc2);
        model.addAttribute("allService2",allService2);
        List<Card> allCard3 = iStaffService.findAllCard32(fIndstaff_tc);
        int tc3=0;
        if (allCard3.size()>0){
            for(Card a:allCard3){
                tc3+=a.getCard_tc();
                tc+=a.getCard_tc();
            }
        }
        model.addAttribute("tc3",tc3);
        model.addAttribute("allCard3",allCard3);
        List<Card> allNewscard4 = iStaffService.findAllNewscard42(fIndstaff_tc);
        int tc4=0;
        if (allNewscard4.size()>0){
            for(Card a:allNewscard4){
                tc4+=a.getCard_tc();
                tc+=a.getCard_tc();
            }
        }
        model.addAttribute("tc4",tc4);
        model.addAttribute("allNewscard4",allNewscard4);
        model.addAttribute("tc",tc);
        return "staff_Achievement.jsp";
    }




}
